#!/usr/bin/env python3
import sys
from PyQt4 import QtCore, QtGui, uic
from user import UserService
from ui_note import NoteWindow
from session import Session
import res.qrc_ui

uiName = 'view/login.ui'

class LoginWidget(QtGui.QWidget):

    userService = UserService()

    def __init__(self):
        super(LoginWidget, self).__init__()
        uic.loadUi(uiName, self)
        self.center()
        #self.textEdit.setContextMenuPolicy(QtCore.Qt.NoContextMenu)

    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size =  self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)

    @QtCore.pyqtSlot()
    def login(self):
        email = self.email.text()
        pwd = self.password.text()
        rememberPwd = self.rememberPwd.isChecked()
        if "" == email:
            self.tipLabel.setText("邮箱格式错误")
            return
        elif "" == pwd:
            self.tipLabel.setText("请输入密码")
            return
        print('%s:%s-%s' % (email, pwd, rememberPwd))
        if not self.userService.checkUser(email, pwd):
            self.tipLabel.setText("邮箱或者密码错误")
            return
        else:
            Session().setAttribute('user', email)
            self.hide()
            
            noteWindow = NoteWindow(self)
            noteWindow.show()
            




if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    view = LoginWidget()
    view.show()
    sys.exit(app.exec_())
